package com.iflytek.autofly.setting.widget;/*
 * ${file_name}<br/>
 * 包含类名列表<br/>
 * 版本信息<br/>
 * date ${date} ${time}<br/>
 * Copy Right
 */

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;

/**
 * CycleView
 *
 * @author ydwang3<br/>
 *         description: 圆形加载View，自带动画效果<br/>
 *         create: 2016/1/7 19:52<br/>
 *         <p/>
 *         change by ydwang3, 2016/1/7 19:52, reason: TODO
 */
public class CycleView extends View {

    /**
     * 中间到边缘跨度
     */
    private static final float CENTER_POLE_SPAN = 100;

    /**
     * 半程移动动画时长
     */
    private static final int HALF_MOVING_DURATION = 1000;

    /**
     * 停止时动画时长
     */
    private static final int STILL_ANIM_DURATION = 500;

    private static final int CIRCLE_MAX_RADIUS = 18;

    private static final int CIRCLE_MIN_RADIUS = 5;
    private static final java.lang.String TAG = "CycleView";

    /**
     * 移动时圆形半径
     */
    private float radius;

    /**
     * 停止时内部透明圆形半径
     */
    private float innerRadius;

    private Paint wPaint;

    /**
     * 当前移动/停止
     */
    private boolean still = false;

    private AnimatorSet anim;

    public CycleView(Context context) {
        this(context, null);
    }

    public CycleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CycleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        wPaint = new Paint();
        wPaint.setStyle(Paint.Style.FILL);
        wPaint.setAntiAlias(true);
        wPaint.setColor(Color.WHITE);
        setWillNotDraw(false);
        initAnim();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (still) {
            wPaint.setStrokeWidth(radius - innerRadius);
            if (radius - innerRadius < 1f) {
                wPaint.setColor(Color.TRANSPARENT);
                wPaint.setStyle(Paint.Style.FILL);
                canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, wPaint);
            } else {
                wPaint.setColor(Color.WHITE);
                wPaint.setStyle(Paint.Style.STROKE);
                canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius - (radius - innerRadius) / 2, wPaint);
            }
        } else {
            wPaint.setColor(Color.WHITE);
            wPaint.setStyle(Paint.Style.FILL);
            canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, wPaint);
        }

//        canvas.drawCircle(getWidth()/2, getHeight()/2, innerRadius, tPaint);
        super.onDraw(canvas);
    }

    public void startAnim() {

        anim.start();
    }

    public void stopAnim() {
        anim.cancel();
    }

    private void setRadius(float radius) {
        still = false;
        this.radius = radius;
        this.postInvalidate();
    }

    private void setInnerRadius(float radius) {
        still = true;
        this.innerRadius = radius;
        this.postInvalidate();
    }

    private void initAnim() {
        ObjectAnimator leftTranslationAnim = ObjectAnimator.ofFloat(this, "translationX", 0, -CENTER_POLE_SPAN, 0);
        leftTranslationAnim.setDuration(HALF_MOVING_DURATION);
        leftTranslationAnim.setInterpolator(new DecelerateInterpolator());

        ObjectAnimator rightTranslationAnim = ObjectAnimator.ofFloat(this, "translationX", 0, CENTER_POLE_SPAN, 0);
        rightTranslationAnim.setDuration(HALF_MOVING_DURATION);
        rightTranslationAnim.setInterpolator(new DecelerateInterpolator());

        ObjectAnimator radiusAnimator = ObjectAnimator.ofFloat(this, "radius", CIRCLE_MAX_RADIUS,
                CIRCLE_MIN_RADIUS, CIRCLE_MAX_RADIUS);
        radiusAnimator.setDuration(HALF_MOVING_DURATION);
        radiusAnimator.setInterpolator(new DecelerateInterpolator());

        AnimatorSet leftTransSet = new AnimatorSet();
        leftTransSet.playTogether(leftTranslationAnim, radiusAnimator);

        AnimatorSet rightTransSet = new AnimatorSet();
        rightTransSet.playTogether(rightTranslationAnim, radiusAnimator.clone());

        ObjectAnimator innerAnim = ObjectAnimator.ofFloat(this, "innerRadius", 0, CIRCLE_MAX_RADIUS,
                CIRCLE_MAX_RADIUS);
        innerAnim.setDuration(STILL_ANIM_DURATION);

        anim = new AnimatorSet();
        anim.playSequentially(leftTransSet, rightTransSet, innerAnim);
        anim.addListener(new Animator.AnimatorListener() {

            private boolean mCanceled;

            @Override
            public void onAnimationStart(Animator animation) {
                mCanceled = false;
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                mCanceled = true;
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (!mCanceled) {
                    animation.start();
                }
            }
        });
    }
}
